package com.yonyou.pmclouds.procedureinspect.process;

import java.util.Date;
import java.util.List;

import com.yonyou.pmclouds.basecom.billstatus.BillStatusConst;
import com.yonyou.pmclouds.basecom.bp.processor.SingleProcessor;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.procedureinspect.mapper.ProcedureInspectBodyMapper;
import com.yonyou.pmclouds.procedureinspect.mapper.ProcedureInspectMapper;
import com.yonyou.pmclouds.processinspect.entity.ProcedureInspectVO;

public class PrcInspectBeforeInsertProcess implements SingleProcessor<ProcedureInspectVO> {

    private ProcedureInspectMapper mapper;

    private ProcedureInspectBodyMapper bodyMapper;

    public PrcInspectBeforeInsertProcess(ProcedureInspectMapper mapper, ProcedureInspectBodyMapper bodyMapper) {
        this.mapper = mapper;
        this.bodyMapper = bodyMapper;
    }

    @Override
    public void process(ProcedureInspectVO vo, ProcedureInspectVO orig) throws BusinessException {
        checkConstructRecord(vo);
        fillBusiInfo(vo);
    }

    /**
     * 方法功能描述：填写必要的业务信息。
     * <p>
     * <b>参数说明</b>
     *
     * @param vo
     * @author zhangwce
     * @time 2017年11月15日 下午3:20:47
     */
    private void fillBusiInfo(ProcedureInspectVO vo) {
        vo.setInspect_date(new Date());
        vo.setBill_type(BillTypeConst.PROCEDUREINSPECT_BILLTYPE);
        // 新增的单据状态为自由态
		vo.setBillstatus(BillStatusConst.COMMON_FREE_STATUS);
    }

    /**
     * 方法功能描述：查询施工记录是否已经被报验过。
     * <p>
     * <b>参数说明</b>
     *
     * @param vo
     * @throws BusinessException
     * @author zhangwce
     * @time 2017年11月15日 下午2:42:44
     */
    private void checkConstructRecord(ProcedureInspectVO vo) throws BusinessException {

        String[] pkRecords = vo.getPk_records();

        List<String> existRecordList = bodyMapper.queryInspectedRecord(pkRecords, BillStatusConst.COMMON_REJECT_STATUS);
        if (existRecordList != null && existRecordList.size() > 0)
            throw new BusinessException("存在已经报验过的施工记录，请重新选择！");
    }


}
